package dao.Frame;



import java.awt.Color;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.EmptyBorder;

import dao.hero.BaseHero;


import javax.swing.ImageIcon;
















import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;

import javax.swing.JTextArea;
import javax.swing.JButton;

import timer.delay_timer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

/**
 * @author 张新林
 * 时间 2015年3月22日下午3:20:28
 * 包名 dao.Frame
 * 类名 MainFrame
 * 游戏的主界面，显示两个英雄通过技能打斗对决的主界面
 */
public class MainFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	JProgressBar progressbar1;
	JProgressBar progressbar2; //血条
	JProgressBar bluebar1;     //蓝条
	JProgressBar bluebar2;
	static int keyskill = 1;
	BaseHero hero;
	BaseHero hero2;
	private JPanel panel;
	//private JLabel skill3;
	private Timer timer=new Timer();
	private JButton skillBtn1;
	private JButton skillBtn2;
	private JButton skillBtn3;
	private JButton skillBtn4;
	JLabel tipNum;
	JTextArea explainText;//技能解释文本
	private final JLabel backgroun = new JLabel("New label");
	private JLabel enemylabel;
	private JLabel meLabel;
	
	 MenuBar mb;  
	 Menu menuFile;  
	 MenuItem mItemNew, mItemOpen, mItemSave; 
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		           
//					MainFrame frame = new MainFrame();
//					
//					frame.setVisible(true);
//					   hero.setLastHP(1000);
//					   hero.setHP(689);
//					   reshowHP(hero,progressbar1);
	}

	/**
	 * Create the frame.
	 */
	public MainFrame(BaseHero hero,BaseHero hero2) {
		setTitle("英雄对决-决斗");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 496, 427);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		//构建英雄
		this.hero = hero;
		this.hero2 = hero2;
		panel = new JPanel();
		panel.setBounds(0, 0, 487, 389);
		contentPane.add(panel);
		//contentPane.setLayout(null);  
		   panel.setLayout(null);
		   initprogressbar();     //初始化血条、蓝条
		   panel.add(progressbar1);
		   panel.add(progressbar2);
		   panel.add(bluebar1);
		   panel.add(bluebar2);
		   
		   tipNum = new JLabel("我方回合");
		   tipNum.setBackground(Color.GREEN);
		   tipNum.setForeground(Color.RED);
		   tipNum.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 14));
		   tipNum.setBounds(0, 49, 63, 22);
		   panel.add(tipNum);
		   
		   JLabel heroname = new JLabel();
		   heroname.setBounds(10, 5, 43, 30);
		   heroname.setFont(new Font("楷体", Font.BOLD, 16));
		   heroname.setText(hero.getName());
		   panel.add(heroname);
		   
		   JLabel heroname2 = new JLabel();
		   heroname2.setBounds(262, 5, 43, 30);
		   heroname2.setFont(new Font("楷体", Font.BOLD, 16));
		   heroname2.setText(hero2.getName());
		   panel.add(heroname2);
		   
		   //技能解释文本
		   explainText = new JTextArea();
		   explainText.setLineWrap(true);
		   explainText.setBounds(307, 321, 170, 68);
		   panel.add(explainText);
		   explainText.setText(hero.getDescribe_skill1());
		   explainText.setEditable(false);
		   
		   JLabel headlabel = new JLabel("");
		   headlabel.setBounds(0, 321, 63, 68);
		   panel.add(headlabel);
		   headlabel.setIcon(new ImageIcon(hero.getHeadpath()));
		   
		   JLabel line = new JLabel("New label");
		   line.setBounds(297, 321, 1, 68);
		   panel.add(line);
		   line.setBackground(Color.BLACK);
		   line.setBorder(BorderFactory.createLineBorder(Color.gray));
		   line.setForeground(Color.BLACK);	   
		   
		   //开始攻击
//		   hero.setLastHP(1000);
//		   hero.setHP(689);
//		   reshowHP(hero,progressbar1);
		   
		   
		   skillBtn1 = new JButton("New button");
		   skillBtn1.setBackground(Color.ORANGE);
		   skillBtn1.setText(hero.getSname1());
		   skillBtn1.setBounds(60, 321, 118, 34);
		   panel.add(skillBtn1);
		   
		   skillBtn2 = new JButton("New button");
		   skillBtn2.setText(hero.getSname2());
		   skillBtn2.setBackground(Color.white);
		   skillBtn2.setBounds(175, 321, 118, 34);
		   panel.add(skillBtn2);
		   
		   skillBtn3 = new JButton("New button");
		   skillBtn3.setText(hero.getSname3());
		   skillBtn3.setBackground(Color.white);
		   skillBtn3.setBounds(60, 355, 118, 34);
		   panel.add(skillBtn3);
		   
		   skillBtn4 = new JButton("New button");
		   skillBtn4.setText(hero.getSname4());
		   skillBtn4.setBackground(Color.white);
		   skillBtn4.setBounds(175, 355, 118, 34);
		   panel.add(skillBtn4);
		   
		   meLabel = new JLabel("");
		   meLabel.setIcon(new ImageIcon(hero.getBodypath()));
		   meLabel.setBounds(110, 130, 81, 86);
		   panel.add(meLabel);
		   
		   enemylabel = new JLabel("");
		   enemylabel.setIcon(new ImageIcon("image/part2.png"));
		   enemylabel.setBounds(297, 130, 75, 86);
		   panel.add(enemylabel);
		   backgroun.setIcon(new ImageIcon("image/back1.jpg"));
		   backgroun.setBounds(0, 0, 477, 329);
		   panel.add(backgroun);
		   
		   
		   PanelgetFocus();   //固定面板焦点
		   regiserListener(); //注册鼠标监听事件
		   //键盘事件
			panel.addKeyListener(new KeyAdapter()
			{
				public void keyPressed(KeyEvent e)
				{
					if(e.getKeyCode()==KeyEvent.VK_UP)
					{
						   
						 //JOptionPane.showMessageDialog(null, keyskill+"");
//						   hero.setLastHP(1000);
//						   hero.setHP(689);
//						   reshowHP(hero,progressbar1);
						 keyskill -= 2;	 
						 if(keyskill==-1)
							 keyskill=3;
						 else if(keyskill==0)
							 keyskill=4;
						 Changeskill();
					}else if(e.getKeyCode()==KeyEvent.VK_DOWN)
					{	
						 keyskill += 2;
						 if(keyskill==5)
							 keyskill=1;
						 else if(keyskill==6)
							 keyskill=2;
						 Changeskill();
					}else if(e.getKeyCode()==KeyEvent.VK_LEFT)
					{
						 keyskill--;
						 if(keyskill==0)
							 keyskill=4;
						 Changeskill();
					}else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
					{
						 keyskill++;
						 if(keyskill==5)
							 keyskill=1;
						 Changeskill();
					}
				}
			});
			//initMenu();//初始化菜单
			setLocationRelativeTo(null);//设置居中
	}
	
	//TODO 切换技能
	public void Changeskill()
	{
		if(keyskill==1)
		{
			skillBtn1.setBackground(Color.ORANGE);
			skillBtn2.setBackground(Color.white);
			skillBtn3.setBackground(Color.white);
			skillBtn4.setBackground(Color.white);
			explainText.setText(hero.getDescribe_skill1());
		}else if(keyskill==2){
			skillBtn1.setBackground(Color.white);
			skillBtn2.setBackground(Color.ORANGE);
			skillBtn3.setBackground(Color.white);
			skillBtn4.setBackground(Color.white);
			explainText.setText(hero.getDescribe_skill2());
		}else if(keyskill==3){
			skillBtn1.setBackground(Color.white);
			skillBtn2.setBackground(Color.white);
			skillBtn3.setBackground(Color.ORANGE);
			skillBtn4.setBackground(Color.white);
			explainText.setText(hero.getDescribe_skill3());
		}else if(keyskill==4){
			skillBtn1.setBackground(Color.white);
			skillBtn2.setBackground(Color.white);
			skillBtn3.setBackground(Color.white);
			skillBtn4.setBackground(Color.ORANGE);
			explainText.setText(hero.getDescribe_skill4());
		}
	}
	
	//TODO 初始化血条函数
	public void initprogressbar()
	{
		   progressbar1 = new JProgressBar();
		   progressbar1.setBounds(45, 4, 180, 30);
		   progressbar1.setOrientation(JProgressBar.HORIZONTAL);//进度条水平显示
		   progressbar1.setMinimum(0);
		   progressbar1.setMaximum((int)hero.getHP());
		   progressbar1.setValue((int)hero.getHP());
		   progressbar1.setStringPainted(true);//显示进度条里的百分值
		   progressbar1.setString(String.valueOf(progressbar1.getValue()));
		   //progressbar.setPreferredSize(new Dimension(150,30));
		   progressbar1.setBorderPainted(true);
		   progressbar1.setBackground(Color.white);//改变进度条背景颜色
		   progressbar1.setForeground(Color.green);
		   
		   progressbar2 = new JProgressBar();
		   progressbar2.setBounds(297, 4, 180, 30);
		   progressbar2.setOrientation(JProgressBar.HORIZONTAL);//进度条水平显示
		   progressbar2.setMinimum(0);
		   progressbar2.setMaximum((int)hero2.getHP());
		   progressbar2.setValue((int)hero2.getHP());
		   progressbar2.setStringPainted(true);//显示进度条里的百分值
		   progressbar2.setString(String.valueOf(progressbar2.getValue()));
		   //progressbar.setPreferredSize(new Dimension(150,30));
		   progressbar2.setBorderPainted(true);
		   progressbar2.setBackground(Color.white);//改变进度条背景颜色
		   progressbar2.setForeground(Color.green);
		   
		   //蓝
		   bluebar1 = new JProgressBar();
		   bluebar1.setBounds(45, 30, 180, 22);
		   bluebar1.setOrientation(JProgressBar.HORIZONTAL);//进度条水平显示
		   bluebar1.setMinimum(0);
		   bluebar1.setMaximum(hero.getBlue());
		   bluebar1.setValue(hero.getBlue());
		   bluebar1.setStringPainted(true);//显示进度条里的百分值
		   bluebar1.setString(String.valueOf(bluebar1.getValue()));
		   bluebar1.setBorderPainted(true);
		   bluebar1.setBackground(Color.white);//改变进度条背景颜色
		   bluebar1.setForeground(Color.blue);
		   
		   bluebar2 = new JProgressBar();
		   bluebar2.setBounds(297, 30, 180, 22);
		   bluebar2.setOrientation(JProgressBar.HORIZONTAL);//进度条水平显示
		   bluebar2.setMinimum(0);
		   bluebar2.setMaximum(hero2.getBlue());
		   bluebar2.setValue(hero2.getBlue());
		   bluebar2.setStringPainted(true);//显示进度条里的百分值
		   bluebar2.setString(String.valueOf(bluebar2.getValue()));
		   bluebar2.setBorderPainted(true);
		   bluebar2.setBackground(Color.white);//改变进度条背景颜色
		   bluebar2.setForeground(Color.blue);
	}
	
	/* TODO
	 * hero第一个为被攻击者
	 * myhero为发起攻击者
	 * progressbar为对方血条
	 * myprogressbar为己方血条
	 * bluebar为己方蓝条
	 */
	public void reshowHP(BaseHero hero,BaseHero myhero,JProgressBar progressbar,JProgressBar myprogressbar,JProgressBar bluebar)
	{
		//己方英雄先减蓝
		bluebar.setValue(myhero.getBlue());
		bluebar.setString(String.valueOf(bluebar.getValue()));
		myprogressbar.setValue((int)myhero.getHP());
		myprogressbar.setString(String.valueOf(myprogressbar.getValue()));
		
		//敌方英雄掉血
		if(myhero.isBlood()){
			timer.cancel();
			timer=new Timer();    //设置定时器
			timer.schedule(new delay_timer(hero,progressbar,timer,myhero.isBlood()), 0, 5);//每40毫秒启动一次
			
		}else{

			timer.cancel();
			timer = new Timer(); // 设置定时器
			timer.schedule(new delay_timer(myhero, myprogressbar, timer,myhero.isBlood()), 0, 5);// 每40毫秒启动一次
		}
		
		
		
//		while(true)
//		{
//			if(SLEEP.flag==false)
//			timer.cancel();
//		}
		
	}
	
	//TODO 
	public void PanelgetFocus()
	{
		panel.setFocusable(true);//使面板获取焦点
		skillBtn1.setFocusable(false);
		skillBtn2.setFocusable(false);
		skillBtn3.setFocusable(false);
		skillBtn4.setFocusable(false);
		explainText.setFocusable(false);
	}
	
	//为技能按钮注册动作监听事件
	public void regiserListener()
	{
		skillBtn1.addActionListener(new skillAction(this));
		skillBtn2.addActionListener(new skillAction(this));
		skillBtn3.addActionListener(new skillAction(this));
		skillBtn4.addActionListener(new skillAction(this));
		
		//为鼠标移动注册监听事件
		skillBtn1.addMouseMotionListener(new MouseMotion(this));
		skillBtn2.addMouseMotionListener(new MouseMotion(this));
		skillBtn3.addMouseMotionListener(new MouseMotion(this));
		skillBtn4.addMouseMotionListener(new MouseMotion(this));
	}
	
	//TODO 显示对话框
	public void showMessage(String str)
	{
		JOptionPane.showMessageDialog(null,str);
	}
	//TODO
	public boolean ifWin(){
		if(hero.getHP()<=0){
			showMessage("战斗结束! "+hero2.getName()+"胜出");
			
		}else if(hero2.getHP()<=0){
			showMessage("战斗结束! "+hero.getName()+"胜出");	
		}
		if(hero.getHP()<=0 || hero2.getHP()<=0){
			showMessage("确定返回主菜单");
			StartFrame startFrame=new StartFrame();
			startFrame.setVisible(true);
			dispose();
			return true;
		}
		return false;
	}

	//TODO 技能按钮注册动作监听类
	class skillAction implements ActionListener{
		private MainFrame mframe;
		skillAction(MainFrame mframe)
		{
			this.mframe=mframe;
		}
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(e.getSource()==skillBtn1){
				mframe.Attck_course(1);
			}else if(e.getSource()==skillBtn2){
				Attck_course(2);
			}else if(e.getSource()==skillBtn3){
				Attck_course(3);
			}else if(e.getSource()==skillBtn4){
				Attck_course(4);
			}
			tipNum.setText("我方回合");
		}
		
	}
	//TODO 攻击流程
	public void Attck_course(int i){
		if(hero.getDEX()>hero2.getDEX()){//根据双方敏捷力决定进攻的优先顺序
			hero.attack(i, hero2);
		    reshowHP(hero2,hero,progressbar2,progressbar1,bluebar1);
		    showMessage("我方回合："+hero.tip);
		    if(ifWin()){
		    	return;
		    }
		    
		    tipNum.setText("敌方回合");
		    //暂停800毫秒
		    try {
				Thread.sleep(800);
			}catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		  //敌人攻击流程
		    int sk=(int)(Math.random()*5+1);//敌方随机技能
		    if(sk==5)
		    	sk=2;//增加2技能的使用概率
		    hero2.attack(sk, hero);
		    showMessage("敌方回合："+hero2.tip);
			reshowHP(hero,hero2,progressbar1,progressbar2,bluebar2);
			if(ifWin()){
		    	return;
		    }
		}else{//敌方优先进攻
			//敌人攻击流程
			tipNum.setText("敌方回合");
		    int sk=(int)(Math.random()*5+1);//敌方随机技能
		    if(sk==5)
		    	sk=2;//增加2技能的使用概率
		    hero2.attack(sk, hero);
		    showMessage("敌方回合："+hero2.tip);
			reshowHP(hero,hero2,progressbar1,progressbar2,bluebar2);
			if(ifWin()){
		    	return;
		    }
			hero.attack(i, hero2);
		    reshowHP(hero2,hero,progressbar2,progressbar1,bluebar1);
		    showMessage("我方回合："+hero.tip);
		    if(ifWin()){
		    	return;
		    }
		    
		    tipNum.setText("我方回合");
		    //暂停800毫秒
		    try {
				Thread.sleep(800);
			}catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	    
	}
	
	class MouseMotion implements MouseMotionListener{
		private MainFrame mframe;
		public MouseMotion(MainFrame mframe) {
			// TODO Auto-generated constructor stub
			this.mframe = mframe;
		}

		public void mouseDragged(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		public void mouseMoved(MouseEvent e) {
			// TODO Auto-generated method stub
			if(e.getSource()==skillBtn1){
				MainFrame.keyskill=1;
				mframe.Changeskill();
			}else if(e.getSource()==skillBtn2){
				MainFrame.keyskill=2;
				mframe.Changeskill();
			}else if(e.getSource()==skillBtn3){
				MainFrame.keyskill=3;
				mframe.Changeskill();
			}else if(e.getSource()==skillBtn4){
				MainFrame.keyskill=4;
				mframe.Changeskill();
			}
		}
		
	}
	
	//初始化菜单
	public void initMenu(){
		mb = new MenuBar(); // 创建菜单栏MenuBar
		menuFile = new Menu("文件");   
		  
        mItemNew = new MenuItem("新建");  
        mItemOpen = new MenuItem("打开");  
          
        menuFile.add(mItemNew);  
        mItemSave = new MenuItem("保存");  
          
        menuFile.add(mItemOpen);  
        // 加入分割线  
        menuFile.addSeparator();  
        menuFile.add(mItemSave);  
        mb.add(menuFile);  
        setMenuBar(mb); 
	}
}
	


